package com.server.tools;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
* @author	李小布
* @email	397554852@qq.com
* @version	创建时间：2018年12月10日 上午10:33:57
*/
public class TimeUtils {
	/**
	 * 将时间戳转换为yyyyMMdd格式短时间
	 * @param TimeStamp
	 * @return
	 */
	public static int TimeStampToShortDate(long TimeStamp) {
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
		Date date = new Date(TimeStamp);
		int shortDate = Integer.parseInt(simpleDateFormat.format(date));
		return shortDate;
	}
	
	/***
	 * yyyyMMdd格式短时间转时间戳
	 * @param shortDate
	 * @return
	 */
	public static long ShortDateToStamp(int shortDate) {
		 long TwelveStamp = -1;
		 DateFormat df = new SimpleDateFormat("yyyyMMdd");
		 try {
			TwelveStamp = df.parse(shortDate+"").getTime();
		 } catch (ParseException e) {
			e.printStackTrace();
			TwelveStamp = -1;
		 }
		 return TwelveStamp;
	 }
	
	/**
	 * 比较两个时间戳天数差
	 * @param time1
	 * @param time2
	 * @return
	 * @throws Exception
	 */
	 public static long getDistanceDays(long time1, long time2) throws Exception{
		 long days=0;
		 long diff ;
		 diff = GetStampWithTwelveStamp(time1) - GetStampWithTwelveStamp(time2);     
		 days = diff / (1000 * 60 * 60 * 24);
	     return days;  
	 }
	 
	 /**
	  * 将时间戳转换为当天0点的时间戳
	  * @param timeStamp
	  * @return
	  */
	 public static long GetStampWithTwelveStamp(long timeStamp) {
		 long TwelveStamp = -1;
		 int shortDate = TimeStampToShortDate(timeStamp);
		 DateFormat df = new SimpleDateFormat("yyyyMMdd");
		 try {
			TwelveStamp = df.parse(shortDate+"").getTime();
		 } catch (ParseException e) {
			e.printStackTrace();
			TwelveStamp = -1;
		 }
		 return TwelveStamp;
	 }
	 
	 /**
	  * Date转yyyy-MM-dd HH:mm:ss格式字符串
	  * @param date
	  * @return
	  */
	 public static String DateToString(Date date) {
		 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		 return sdf.format(date);
	 };
	 
	 /**
	  * yyyy-MM-dd HH:mm:ss格式字符串转date
	  * @param dateString
	  * @return 返回null，格式不正确
	  */
	 public static Date StringToDate(String dateString) {
		 Date date = null;
		 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		 try {
			date = sdf.parse(dateString);
		 } catch (ParseException e) {
			//e.printStackTrace();
		 }
		 return date;
	 };
	 
	 /**
	  * 时间戳转yyyy-MM-dd HH:mm:ss格式字符串
	  * @param timeStamp
	  * @return
	  */
	 public static String TimestampToDateString(long timeStamp){
		 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		 Date date = new Date(timeStamp);
		 String dateString = sdf.format(date);
		 return dateString;
	 }
	 
	 /**
	 * yyyy-MM-dd HH:mm:ss格式字符串转时间戳
	 * @param dateString
	 * @return 返回-1 参数格式错误
	 */
	 public static Long DateStringToTimestamp(String dateString){
		 Date date = StringToDate(dateString);
		 if(date == null) {
			 return -1L;
		 }
		 return date.getTime();
	 }
	 
	 public static void main(String[] args) {
		 System.err.println(DateStringToTimestamp("2018-09-2312:37:15"));
	 }
}
